package Order;

import Fruit.Fruit;
import Strategy.Strategy;

import java.util.List;
import java.util.Objects;

/**
 * 订单类
 */
public class Order {

    private List<Fruit> fruits;
    private Strategy strategy;

    public Order(List<Fruit> fruits) {
        setFruits(fruits);
        setStrategy(new Strategy() {});
    }

    public Order(List<Fruit> fruits, Strategy strategy) {
        setFruits(fruits);
        setStrategy(strategy);
    }

    public Strategy getStrategy() {
        return strategy;
    }

    public void setStrategy(Strategy strategy) {
        if (Objects.isNull(strategy)) {
            this.strategy = new Strategy() {};
        } else {
            this.strategy = strategy;
        }
    }

    public List<Fruit> getFruits() {
        return fruits;
    }

    public void setFruits(List<Fruit> fruits) {
        if (Objects.isNull(fruits) || fruits.isEmpty()) throw new RuntimeException("传入水果列表不能为空");

        this.fruits = fruits;
    }

    /**
     * 计算订单总价格
     */
    public Double computeSum(){
        if (
            Objects.isNull(this.fruits)
            || this.fruits.isEmpty()
        )
            return 0.0;

        return strategy.sum(this.fruits);
    }
}
